package control;

public class RandomQPRS {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length == 0){
			System.out.println("Debug Mode");
			System.out.println("Try using --help to know how to use this application");
			//generateQuatTreeEdges();
			runAntColonySystem();			
		}
		else {
			if(args.length < 1) {
				System.out.println("Wrong number of arguments");
				return;
			}
			if(args[0] == "--help" || args[0] == "-h"){
				System.out.println("Usage: qprs <arguments>\n\n");
				System.out.println("The commands to use are:");
				System.out.println("   --help     duh!");
				System.out.println("   --graph    generates the random graphs with random obstacles and put the objects in binary files.");
				System.out.println("   --acs      run the ant colony system to get the best path for each random graph\n\n");
				System.out.println("The output results are generated in 'result' directory.");
			} else if(args[0] == "--graph" || args[0] == "-g") {
				generateQuatTreeEdges();				
			} else if(args[0] == "--acs" || args[0] == "-a") {
				runAntColonySystem();
			} else if(args.length == 2) {
				if((args[0] == "--graph" || args[0] == "-g") && (args[0] == "--acs" || args[0] == "-a")){
					generateQuatTreeEdges();
					runAntColonySystem();
				} else {
					System.out.println("Wrong atributes!");
					return;
				}
			} else {
				System.out.println("Something wrong with yout parameters. Try qprs --help to get help.");
			}
		}
	}
	
	private static void runAntColonySystem(){
		RandomACS acsQPRS = new RandomACS();
		acsQPRS.bashRunACS();
	}
	
	private static void generateQuatTreeEdges(){
		System.out.println("Creating random QuadTree for AntColony...");
		RandomGraph quadTree = new RandomGraph();
		quadTree.generateGraph();
		System.out.println("Ok, done!");
	}
	
	/*private static void bashRunACS(){
		double d[][] = null;
		int t[] = null;
		try {
			acsOuts = new PrintStream(new FileOutputStream(Config.workDir + "test_results.csv"));
			acsOuts.println("Id,Ants,Tree_Nodes,Iterations,Path,Time(s),Distance,Path_Nodes");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}		
		
		for(int i = 0; i < RandomGraph.getNumberOfGaphs(); i++){
			try {     
				// Rotina de leitura do resultado do QuadTree			
				ObjectInputStream ins = new ObjectInputStream(new FileInputStream(Config.workDir + i + "_random_graph.bin"));
				d = (double[][])ins.readObject();
				ins.close();
				ins = new ObjectInputStream(new FileInputStream(Config.workDir + i + "_random_target.bin"));
				t = (int[])ins.readObject();
			} catch(Exception ex) {
				ex.printStackTrace();
			}
			runAntColonySystem(d, t, i);
		}
	}	
	
	private static void runAntColonySystem(double[][] deltas, int[] targets, int id){
		System.out.println("Running AntColonySystem for QPRS for Test " + id);        

		int nAnts = 100;
		int nNodes = -1;
		int nIterations = 50;
		int nRepetitions = 1;        

		double d[][] = deltas;

		nNodes = d.length;	
		AntGraph graph = new AntGraph(nNodes, d);
		
		int nNodesInZArray[] = targets;
		
		for(int i = 0; i < nRepetitions; i++)
		{
			AntColony4QPRS antColony = new AntColony4QPRS(graph, nNodesInZArray, nAnts, nIterations);
			long time = System.currentTimeMillis();
			antColony.start();
			time = System.currentTimeMillis() - time;
			if(antColony.getBestPath()!=null){
				System.out.println("Best path found for test " + (id+1) + " in round " + (i+1));
				String path = "";
				for(int k = 0; k < antColony.getBestPath().length; k++)
					path += antColony.getBestPath()[k] + " ";
				acsOuts.println((id+1) + "," + nAnts + "," + nNodes + "," + nIterations + "," + path + "," + (time/1000) + "," + antColony.getBestPathValue() + "," + antColony.getBestPath().length);
			}
			else System.out.println("There's no path from "+nNodesInZArray[0]+" to "+nNodesInZArray[1] + "\n");
			graph.resetTau();
		}		
	}

	/*private static void runQPRS(int x) {
		int num = x + 1;
		System.out.println("Running test " + num);
		
		RandomGraph mainProg = new RandomGraph();
		mainProg.generateGraph();
		System.out.println("Number of Obstacles: " + mainProg.getObstacles().getObstacles().size());
		System.out.println("Number of Edges: " + mainProg.getEdges().length);
		System.out.println("Number of Leaf Nodes: " + mainProg.getTree().getLeafNodes().size());

		// Print application prompt to console.
		System.out.println("\nAntColonySystem for QPRS");        

		int nAnts = 100;
		int nNodes = -1;
		int nIterations = 50;
		int nRepetitions = 1;        

		double d[][] = mainProg.getEdges();

		nNodes = d.length;	
		AntGraph graph = new AntGraph(nNodes, d);
		
		int nNodesInZArray[] = mainProg.getTargetSource();

		for(int i = 0; i < nRepetitions; i++)
		{
			AntColony4QPRS antColony = new AntColony4QPRS(graph, nNodesInZArray, nAnts, nIterations);
			long time = System.currentTimeMillis();
			antColony.start();
			time = System.currentTimeMillis() - time;
			if(antColony.getBestPath()!=null){
				//System.out.println(i + "," + antColony.getBestPathValue() + "," + antColony.getLastBestPathIteration() + "," + antColony.getBestPath().length);
				System.out.println("Total running time: " + time/1000 + " seconds");
				System.out.print("Path {");
				for(int k = 0; k < antColony.getBestPath().length; k++)
					System.out.print(antColony.getBestPath()[k] + " ");
				System.out.println("}\n");
			}
			else System.out.println("There's no path from "+nNodesInZArray[0]+" to "+nNodesInZArray[1] + "\n");
			graph.resetTau();
		}			
	}*/
}
